import SendMsg from "@/api/SendMsg";
import {message} from "ant-design-vue";
import i18n from "../../../i18n/index";
import router from "@/router";
import {cloneDeep} from 'lodash';

const reprint = {
    namespaced: true,

    state: {
        isDateFlg: "N",
        selectRow: {},
        printCnt: 1,
        rowSelection: {
            selectedRowKeys: [],
            type: 'radio',
            onChange: (selectedRowKeys, selectedRows) => {
                reprint.state.rowSelection.selectedRowKeys = selectedRowKeys;
                reprint.state.selectRow = selectedRows[0];
                reprint.mutations.queryLotIdA (selectedRows[0].lotId);
            },
        },
        paramData: [],
        printerInfo: {
            info: "",
            radioValue: "N",
            radioTemplate: "1",
        },
        reprintVisible: false,
        reprintInfo: {
            lotId: "",
            reprintCount: "",
            startValue: "",
            endValue: "",
        },

        dataSource: [],
        dataPagination: {
            // current: 0,
            pageSize: 13,
            showTotal: (total, range) => {
                return i18n.t('menuItem.total')+` ${total} `+i18n.t('menuItem.term')
            },
        },
    },
    mutations: {
        setDataPagination(state, val) {
            state.dataPagination = val;
        },
        queryType(state) {
            let inTrx = {
                trxId: "bisData",
                actionFlg: "F",
                dataCate: "PRIP",
            };
            SendMsg.sendPostTrx(inTrx).then(res => {
                let data = res.data;
                if ('0000000' === data.rtnCode) {
                    state.paramData = [];
                    state.paramData.push(...data.oaryA);
                }
            });
        },
        changeIsDateFlg(state,e) {
            state.isDateFlg = e.target.value;
        },
        bindingHandleOk(state) {
            reprint.mutations.retPrintInfo(state);
        },
        bindingHandleCancel(state) {
            state.reprintVisible = false;
            state.printerInfo.info = "";
            state.printerInfo.radioValue = "N";
            state.printerInfo.radioTemplate = "1";
        },
        reprint(state) {
            if (state.rowSelection.selectedRowKeys.length == 0) {
                message.warn(i18n.t('chooseLotId'));
                return;
            }
            state.reprintVisible = true;
        },
        clearAll(state) {
            state.rowSelection.selectedRowKeys = [];
            state.dataSource = [];
            state.reprintInfo = {
                lotId: "",
                reprintCount: ""
            };
        },
        queryLotId(state) {
            if (!state.reprintInfo.lotId && !state.reprintInfo.startValue && !state.reprintInfo.endValue){
                return message.warn(i18n.t('pleaseEnterQueryInformation'))
            }
            state.dataSource = [];
            state.rowSelection.selectedRowKeys = [];
            let inTrx = {
                trxId: "REPRINT",
                actionFlg: "Q",
                lotId: state.reprintInfo.lotId,
                startTime: state.reprintInfo.startValue,
                endTime: state.reprintInfo.endValue
            };
            SendMsg.sendPostTrx(inTrx).then(res => {
                let data = res.data;
                if ('0000000' === data.rtnCode) {
                    state.dataSource.push(...data.printInfoOAS);
                    state.reprintInfo.reprintCount = data.count;
                }
            });
        },
        queryLotIdA(lotId) {
            let inTrx = {
                trxId: "REPRINT",
                actionFlg: "QA",
                lotId: lotId,
            };
            SendMsg.sendPostTrx(inTrx).then(res => {
                let data = res.data;
                if ('0000000' === data.rtnCode) {
                    reprint.state.reprintInfo.reprintCount = data.count;
                }
            });
        },
        retPrintInfo(state) {
            let inTrx = {
                trxId: "REPRINT",
                actionFlg: "R",
                printerInfo: state.printerInfo.info,
                printInfoOAS: state.selectRow,
                printCnt:state.printCnt,
                isFirstDate:state.isDateFlg,
                useTemplate: state.printerInfo.radioTemplate
            };
            SendMsg.sendPostTrx(inTrx).then(res => {
                let data = res.data;
                if ('0000000' === data.rtnCode) {
                    state.reprintInfo.reprintCount = data.count;
                    state.printerInfo.info = "";
                    state.reprintVisible = false;
                    state.printerInfo.radioValue = "N";
                    state.printerInfo.radioTemplate = "1";
                    message.success( i18n.t('menu.reprintSuccess'))
                }
            });
        },
    },
    getters: {},
    action: {}
}
export default reprint;
